<?php

namespace App\Http\Controllers\Admin;

use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class ProductController extends Controller
{
    //

    public function index()
    {

        return view('admin.product.index');
    }

    public function indexPost(Request $request)
    {
        $input = $request->all();
        $condition = [
            'condition' => []
        ];
        $search = [];
        if($request->has('serach_name')){
            //dd($request->input('serach_name'));
            $condition = [
                'condition' => [
                    [
                        'where',
                        ['where','name like %?%']
                    ]
                ]
            ];
            $search[0] = $request->input('serach_name');
        }
        $dd = (new Product())->dataTable10(['*'], $input ,$condition, $search);
        return response()->json($dd);
    }

    /**
     *  添加产品
     */
    public function addProduct()
    {

        return view('admin.product.addProduct');
    }

    public function addProductPost(Request $request)
    {
        //dd($request->all());
        $input = [
            'name' => $request->input('name'),
            'description' => $request->input('description'),
            'content' => $request->input('content'),
            'thumb_img' => $request->input('thumb_img'),
            'pro_img' => $request->input('pro_img'),
            'price' => $request->input('price'),
            'price_old' => $request->input('price_old'),
            'nums' => $request->input('nums'),
            'datemin' => $request->input('datemin'),
            'datemax' => $request->input('datemax')
        ];
        if($request->has('id')){
            $res = Product::find($request->input('id'))->update($input);
        }else{
            $res = Product::create($input);
        }

        if($res){
            return response()->json(['code'=>1 ,'msg'=>'成功']);
        }else{
            return response()->json(['code'=>0 ,'msg'=>'失败']);
        }

    }

    /**
     *  编辑产品
     */
    public function editProduct($id = 0)
    {
        $product_info = Product::find($id);

        return view('admin.product.addProduct',['id'=>$id, 'product_info'=>$product_info]);
    }

    /**
     *  删除产品
     */
    public function delProduct(Request $request)
    {
        if($request->has('id')){
            $res = Product::find($request->input('id'))->delete();
            if($res){
                return response()->json(['code'=>1, 'msg'=>'成功']);
            }else{
                return response()->json(['code'=>0, 'msg'=>'失败']);
            }
        }
    }

    /**
     *  图片上传
     */
    public function upload(Request $request)
    {
        if ($request->isMethod('post')) {
            $file = $request->file('file');
            // 文件是否上传成功
            if ($file->isValid()) {
                // 获取文件相关信息
                $originalName = $file->getClientOriginalName(); // 文件原名
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                $type = $file->getClientMimeType();     // image/jpeg
                // 上传文件
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
                if($bool){
                    $res = [
                        'code' => 1,
                        'status' => 'success',
                        'file' => $filename,
                        'path' => public_path('uploads').'/'.$filename,
                    ];
                }else{
                    $res = [
                        'code' => 0,
                        'status' => 'error',
                    ];
                }
                return response()->json($res);
            }

        }
    }


}
